package sunyu.controller.shiro;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sunyu.controller.CommonController;
import sunyu.pojo.ShiroRole;
import sunyu.pojo.easyui.DataGrid;
import sunyu.pojo.easyui.Result;
import sunyu.service.RoleService;
import sunyu.toolkit.core.ExecutorServiceKit;
import sunyu.toolkit.core.LogKit;
import sunyu.toolkit.core.RandomKit;
import sunyu.toolkit.core.StringKit;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author 孙宇
 */
@Controller
@RequestMapping("/shiro/role")
public class RoleController extends CommonController {

    private static final Logger logger = LogKit.getLogger();

    @Resource
    ExecutorServiceKit executorServiceKit;

    @Resource(name = "roleService")
    RoleService roleService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String gridPage() {
        return "/template/shiro/role/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public DataGrid grid(int page, int rows, String sort, String order) {
        DataGrid dg = new DataGrid();
        PageHelper.startPage(page, rows);
        List<ShiroRole> l = roleService.select(null);
        PageInfo p = new PageInfo(l);
        dg.setRows(p.getList());
        dg.setTotal(p.getTotal());
        return dg;
    }

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String formPage(Model model, @RequestParam(required = false) String id) {
        if (StringKit.isNotBlank(id)) {
            Future f1 = executorServiceKit.submit(() -> model.addAttribute("data", roleService.selectByPrimaryKey(id)));

            Future f2 = executorServiceKit.submit(() -> {
                String resourceIds = "";
                for (String resourceId : roleService.selectResourceIds(id)) {
                    resourceIds += "'" + resourceId + "',";
                }
                if (StringKit.isNotBlank(resourceIds)) {
                    model.addAttribute("resourceIds", resourceIds.substring(0, resourceIds.length() - 1));
                }
                return null;
            });

            try {
                f1.get();
                f2.get();
            } catch (InterruptedException e) {
            } catch (ExecutionException e) {
            }
        }
        return "/template/shiro/role/form";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ModelAttribute("data") ShiroRole record, @RequestParam(required = false) List<String> resourceIds) {
        record.setId(RandomKit.UU64());
        Result r = new Result();
        roleService.insertRoleAndResource(record, resourceIds);
        r.setSuccess(true);
        r.setMsg("添加成功！");
        return r;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result edit(@ModelAttribute("data") ShiroRole record, @RequestParam(required = false) List<String> resourceIds) {
        Result r = new Result();
        roleService.updateRoleAndResource(record, resourceIds);
        r.setSuccess(true);
        r.setMsg("编辑成功！");
        return r;
    }

    @RequestMapping("/remove/{id}")
    @ResponseBody
    public Result remove(@PathVariable("id") String id) {
        roleService.deleteByPrimaryKey(id);
        Result r = new Result();
        r.setSuccess(true);
        r.setMsg("删除成功！");
        return r;
    }

}
